package zy1208.sikao;

import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;

public class demo {
    public static void main(String[] args) throws Exception{
        /*
         * 1、
         * Person代码不可以更改！！！
         * 创建一个Person对象，
         * 保存如下数据到对象中：
         * 小张，18,  130302201110010001
         *
         * 调用Person对象的toString()方法在控制台展示数据
         *
         * 2、
         * 将Person对象的数据保存到data2.properties文件中（字段顺序无序即可）
         * name=小张
         * age=18
         * idCardNum=130302201110010001
         */

        Class<?> aClass = Class.forName("zy1208.sikao.Person");
        Constructor<?> c = aClass.getDeclaredConstructor(String.class, int.class, String.class);
        Object o = c.newInstance("小张", 18, "130302201110010001");
        c.setAccessible(true);
        Method ts = aClass.getDeclaredMethod("toString");
        Object o1 = ts.invoke(o);
        System.out.println(o1);
        String str = ""+o1;
        String s = str.substring(8, str.length() - 1);
        String[] split = s.split(", ");
        Properties prop = new Properties();
        StringBuilder str1 = new StringBuilder();
        for (String s1 : split) {
            String[] split1 = s1.split("=");
            prop.setProperty(split1[0], split1[1]);
            str1.append(split1[0]+"="+split1[1]);
        }
        FileWriter f = new FileWriter("data2.properties");
        prop.store(f,str1.toString());
        f.close();
    }
}
